<template>
    <div style="border: 1px solid #ccc">
        <!-- 工具栏 -->
        <Toolbar class="toolbar" :editor="editorRef" :defaultConfig="toolbarConfig"
            :mode="mode" />
        <!-- 编辑器-->
        <Editor class="editor" v-model="valueHtml" :defaultConfig="editorConfig"
            :mode="mode" @onCreated="handleCreated" />
    </div>
</template>

<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor } from '@wangeditor/editor-for-vue'
import { Toolbar } from '@wangeditor/editor-for-vue'
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()
// 内容 HTML
const valueHtml = ref('<p>模拟 Ajax 异步设置内容</p>')

//配置菜单栏
const toolbarConfig = {toolbarKeys:['headerSelect','codeBlock']}

//配置编辑器栏目
const editorConfig = { placeholder: '请输入内容...' }

const handleCreated = (editor) => {
    editorRef.value = editor // 记录 editor 实例，重要！
    
}

const mode = ref('default')// 或 'simple'

// 模拟 ajax 异步获取内容
onMounted(() => {

})







// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})

</script>
<style scoped>
/* 工具栏 */
.toolbar{
    border-bottom: 1px solid #ccc;
    z-index: 100;
}
/* 编辑器 */
.editor{
    height: 500px !important;
    overflow-y: hidden !important;
    z-index: 100;
}
</style>